<script type="text/javascript">
    $('#tags-datagrid').datagrid({
        local: 'remote',
        dataUrl: '/api/tag/list',
        dataType: 'json',
        sortAll: true,
        filterAll: true,
        columns: [{
            name: 'name',
            label: '标签名称',
            align: 'center',
            width: 40,
            render: function(value, obj) {
                // console.log(obj);
                return obj.name;
            }
        }, {
            name: 'slug',
            label: 'URL',
            align: 'center',
            width: 70
        }, {
            name: 'description',
            label: '描述',
            align: 'center',
            width: 70
        }, {
            name: 'created_at',
            label: '创建时间',
            align: 'center',
            type: 'date',
            pattern: 'yyyy-MM-dd HH:mm:ss'
        }, {
            name: 'updated_at',
            label: '更新时间',
            align: 'center',
            type: 'date',
            pattern: 'yyyy-MM-dd HH:mm:ss'
        }],
        hiddenFields: [{
            name: 'id'
        }],
        // editUrl: 'ajaxDone1.html',
        paging: {
            pageSize: 15,
            pageCurrent: 1
        },
        showCheckboxcol: true,
        // showEditBtnsCol: true,
        linenumberAll: true,
        fullGrid: true
    })

    /**
     * 新增标签
     */
    function addTag(obj, type) {

        if (type === 'add') {
            $(obj).dialog({
                id: 'addTag',
                url: '/admin/tag/edit',
                title: '新增标签',
                mask: true,
                width: 800,
                height: 400,
                onClose: function() {
                    refresh(); //刷新列表
                }
            });
        } else {

            var selected = $('#tags-datagrid').data().selectedDatas;
            if (selected == undefined) {
                $(this).alertmsg('warn', '请选择一条记录');
                return;
            }
            var length = selected.length;
            if (length != 1) {
                $(this).alertmsg('warn', '只能选择一条记录');
                return;
            }
            var selectedId;

            selectedId = selected[0].id;

            $(obj).dialog({
                id: 'editTag',
                url: '/admin/tag/edit',
                title: '编辑标签',
                data: {
                    tagId: selectedId
                },
                mask: true,
                width: 800,
                height: 400,
                onClose: function() {
                    refresh(); //刷新列表
                }
            });
        }
    }

    /**
     * 删除标签
     */
    function deleteTag() {

        var selected = $('#tags-datagrid').data().selectedDatas;
        if (selected == undefined) {
            $(this).alertmsg('warn', '请至少选择一条记录');
            return;
        }
        var length = selected.length;
        var selectedDataIds = [];
        // var data = $('#users-datagrid').data().allData;
        for (var i = 0; i < length; i++) {
            selectedDataIds.push(selected[i]['id']);
        }

        $(this).bjuiajax('doAjax', {
            url: '/api/tag/delete',
            type: "post",
            data: {
                tagIds: selectedDataIds.join(',')
            },
            confirmMsg: '确定要删除么？',
            callback: function(result) {
                if (result.statusCode === '200') {
                    $(this).alertmsg('info', result.message);
                    // $(this).navtab('refresh');
                    refresh();
                }
            }
        })
    }

    /**
     * 刷新
     */
    function refresh() {
        $('#tags-datagrid').datagrid('refresh')
    }
</script>
<div class="bjui-pageHeader">
    <div class="pull-right">
        <button type="button" class="btn btn-blue" data-icon="plus" onclick="addTag(this,'add')">添加</button>
        <button type="button" class="btn btn-green" data-icon="edit" onclick="addTag(this,'edit')">编辑</button>
        <button type="button" class="btn btn-red" data-icon="times" onclick="deleteTag(this)">删除</button>
        <button type="button" class="btn btn-green" data-icon="refresh" onclick="refresh(this)">刷新</button>
    </div>
</div>
<div class="bjui-pageContent">
    <div>
        <table id="tags-datagrid" data-width="100%" data-height="500" class="table table-bordered">
        </table>
    </div>
</div>